package bb.lanxing.fragment.routebook;

import static bb.lanxing.manager.SPConstant.KEY_SHOW_IMPORT_LUSHU;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.RequiresApi;

import bb.lanxing.activity.routebook.RouteBookDetailMapActivity;
import bb.lanxing.activity.routebook.RouteBookInfoActivity;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.model.database.RouteBook;

public class RouteBookBaseLocalListFragment extends RouteBookListFragmentNew {
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (Objects.requireNonNull(action)) {
            case RouteBook.ACTION_WORKOUT_CREATE_LUSHU, RouteBook.ACTION_DOWNLOAD_LUSHU, RouteBook.ACTION_DELETE_LUSHU -> {
                SharedManager.getInstance().setCreateRouteBookDownloadIsNeedRefresh(0);
                autoRefresh();
            }
            }
        }
    };

    private boolean showImportRouteBookBtn = false;

    public void setShowImportRouteBookBtn(boolean showImportRouteBookBtn) {
        this.showImportRouteBookBtn = showImportRouteBookBtn;
    }

    @Override
    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(RouteBook.ACTION_DELETE_LUSHU);
        intentFilter.addAction(RouteBook.ACTION_DOWNLOAD_LUSHU);
        intentFilter.addAction(RouteBook.ACTION_WORKOUT_CREATE_LUSHU);
        requireActivity().registerReceiver(this.receiver, intentFilter, Context.RECEIVER_EXPORTED);
    }

    @Override
    public void requestData() {
        refreshUIWithLocal();
    }

    private void refreshUIWithLocal() {
        List<RouteBook> localRouteBook = filterLushu(getLocalRouteBook());
        emptyBackground.setVisibility(localRouteBook.isEmpty() ? View.VISIBLE : View.GONE);
        routeBookList.clear();
        routeBookList.addAll(localRouteBook);
        adapter.notifyDataSetChanged();
        refreshComplete();
    }

    @Override
    public void refresh() {
        if (SharedManager.getInstance().getCreateRouteBookDownloadIsNeedRefresh() == 1) {
            SharedManager.getInstance().setCreateRouteMineIsNeedRefresh(0);
            autoRefresh();
        }
    }

    @Override
    protected Intent getIntent(RouteBook routeBook) {
        Intent intent;
        if (routeBook.getSourceType() != RouteBook.TYPE_DRAW && routeBook.isUploadEnable()) {
            intent = new Intent(getActivity(), RouteBookInfoActivity.class);
            if (showImportRouteBookBtn) {
                intent.putExtra(KEY_SHOW_IMPORT_LUSHU, true);
            }
        } else {
            intent = new Intent(getActivity(), RouteBookDetailMapActivity.class);
        }
        return intent;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        requireActivity().unregisterReceiver(this.receiver);
    }

    private List<RouteBook> getLocalRouteBook() {
        return RouteBook.getList();
    }

    @Override
    protected void setEmptyBackground(boolean isEmpty) {
        emptyBackground.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
    }

    protected List<RouteBook> filterLushu(List<RouteBook> routeBookList) {
        return routeBookList.stream().filter((item) -> item.getServerId() > 0).collect(Collectors.toList());
    }
}
